
import { request, api } from '../../utils/request'
import {formatTotalPrice} from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsId: 0,
    num: 1,
    totalPrice: '0.00',
    goodsInfo: {},
    loading: true,
    show: false,
    buttonType: 'Cart',
    checkedSpecText: {},
    tmpSpecText: {},
    skuInfo: [],
  },
  numChange(params) {
    let totalPrice = formatTotalPrice(this.data.skuInfo.skuPrice, params.detail);
    this.setData({
      num: params.detail,
      totalPrice: totalPrice
    })
  },
  toHome(params) {
    wx.switchTab({
      url: params.currentTarget.dataset.url
    })
  },
  onClickButton(params) {
    this.setData({
      show: true,
      buttonType: params.currentTarget.dataset.url
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  // 规格选择
  clickSkuValue(params) {
    let groupId = params.currentTarget.dataset.groupId;
    let specId = params.currentTarget.dataset.specId;
    // console.log(groupId);
    // console.log(specId)
    
    let _goodsInfo = this.data.goodsInfo;
    let _specificationList = _goodsInfo.specGroupList;
    // console.log(_specificationList);
    
    let specText = "已选择："
    let ids = []
    for (let i = 0; i < _specificationList.length; i++) {
      for (let j = 0; j < _specificationList[i].specInfoList.length; j++) {
        if (_specificationList[i].id === groupId) {
          if (_specificationList[i].specInfoList[j].id === specId) {
            ids.push(_specificationList[i].specInfoList[j].id)
            specText += _specificationList[i].specInfoList[j].name + " "
            _specificationList[i].specInfoList[j].checked = true;
            //选择了，则判断当前是否有图片，且图片内容不能为空，不能为默认lazy图片，则替换
            // that.setSpecificationUrl(_specificationList[i].valueList[j].picUrl);
          } else {
            _specificationList[i].specInfoList[j].checked = false;
          }
        } else if (_specificationList[i].specInfoList[j].checked) {
          ids.push(_specificationList[i].specInfoList[j].id)
          specText += _specificationList[i].specInfoList[j].name + " "
        }
      }
    }
    
    let _skuInfo = this.getSkuInfo(_goodsInfo.skuInfoList, ids)
    let totalPrice = formatTotalPrice(_skuInfo.skuPrice, this.data.num)
    this.setData({
      totalPrice: totalPrice,
      goodsInfo: _goodsInfo,
      skuInfo: _skuInfo,
      tmpSpecText: specText
    });
  },
  async clickBuy() {
    let _skuInfo = this.data.skuInfo;
    
    this.setData({
      show: false
    })
    wx.navigateTo({
      url: '/pages/checkout/checkout?goodsId=' + this.data.goodsId + '&skuId=' + _skuInfo.id + '&num=' + this.data.num
    })
  },
  async getGoodsInfo() {
    // console.log(this.data.id);
    
    let res = await request(api.GoodsDetail + "?goodsId=" + this.data.goodsId);
    // console.log(res);
    if (res.errno === 0) {

      let _specificationList = res.data.specGroupList
      // 如果仅仅存在一种货品，那么商品页面初始化时默认checked
      let specText = '已选择：'
      let ids = []
      for (let i = 0; i < _specificationList.length; i++) {
        if (_specificationList[i].specInfoList.length >= 1) {
          ids.push(_specificationList[i].specInfoList[0].id)
          _specificationList[i].specInfoList[0].checked = true
          specText += _specificationList[i].specInfoList[0].name + " "
        }
      }
      res.data.goodsContent = res.data.goodsContent.replace(/<img /g, '<img style="display:block;width:100%;" ')
      let skuInfo = this.getSkuInfo(res.data.skuInfoList, ids)
      let totalPriceStr = formatTotalPrice(skuInfo.skuPrice, this.data.num);
      this.setData({
        totalPrice: totalPriceStr,
        goodsInfo: res.data,
        skuInfo: skuInfo,
        tmpSpecText: specText,
        loading: false
      });
    }
  },
  getSkuInfo(skuInfoList, ids) {
    ids.sort()
    let specIds = ids.join("_")
    // console.log(skuInfoList);
    for (let i = 0; i < skuInfoList.length; i++) {
      let skuInfo = skuInfoList[i];
      if (specIds === skuInfo.specIds) {
        return skuInfo;
      }
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    
    this.setData({
      goodsId: parseInt(options.id)
    })
    this.getGoodsInfo()
  },
})